package com.abaddon.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import android.util.Log;

import com.abaddon.model.Person;
import com.abaddon.model.TagManager;
import com.google.android.maps.GeoPoint;

public class ServerCommunicationRunnable implements Runnable {

	private int maxMessageLength = 39;
	
	@Override
	public void run() {
		while(true){
			try{
				String name = TagManager.getUserName();
				
				Person person = TagManager.getPerson(name);
				
				byte[] bytes = new byte[39];
				DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(TagManager.getServerAdress()), TagManager.getServerPort());
				DatagramSocket datagramSocket = new DatagramSocket();
				
				if(person != null){
					Log.i("SERVERCOM", "Sending my position to server");
					String message = "mPos:" + person.getTitle() + ":" + person.getPoint().getLatitudeE6() + ":" + person.getPoint().getLongitudeE6() + ":;" ;
					setPackageMessage(datagramPacket, message);
					datagramSocket.send(datagramPacket);
				}
				
				//Request positions
				String toSend = ("rPos:" + name + ":;");
				setPackageMessage(datagramPacket, toSend);
				datagramSocket.send(datagramPacket);

				//Remove all persons
//				PropertyChangeEvent ae = new PropertyChangeEvent(this, AbaddonPropertyChangeValuesHolder.REMOVEALLPERSONS, toSend, null);
//				PropertyChangeSupport pcs = new PropertyChangeSupport(TagManager.getAbaddon());
//				pcs.addPropertyChangeListener(TagManager.getAbaddon());
//				pcs.firePropertyChange(ae);
//				TagManager.removeAllPersons();
				
				//Receive 		
				datagramSocket.setSoTimeout(900);
				String response;
				while(true){
					datagramSocket.receive(datagramPacket);
					response = new String(datagramPacket.getData());
					String[] parsedResponse = response.split(":");
					Log.i("serverCom", response);
					TagManager.updatePerson(parsedResponse[1], Integer.parseInt(parsedResponse[2]), Integer.parseInt(parsedResponse[3]));
				}
				
			} catch (IOException e) {
				Log.i("serverCommunication", "No more packages to read");
				
				//Remove all persons
				PropertyChangeEvent ae = new PropertyChangeEvent(this, AbaddonPropertyChangeValuesHolder.INVALIDATE, new String("2"), null);
				PropertyChangeSupport pcs = new PropertyChangeSupport(TagManager.getAbaddon());
				pcs.addPropertyChangeListener(TagManager.getAbaddon());
				pcs.firePropertyChange(ae);
			}
			
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public ServerCommunicationRunnable() { }
	
	private void setPackageMessage(DatagramPacket dp, String s){
		
		String fillString = "";
		
		for(int i = 0; i<maxMessageLength; i++){
			if(i < s.length()){
				fillString += s.charAt(i);
			}else{
				fillString += "X";
			}
		}
		
		byte[] toFill = fillString.getBytes();
		
		dp.setData(toFill);
			
	}
	
}
